package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseSupport;
import com.mt.roll.core.base.IErrorCode;
import com.mt.roll.core.dal.entity.ScrapSubstrateLog;
import com.mt.roll.core.dal.service.ScrapSubstrateLogServiceBiz;
import com.mt.roll.core.errorcode.ApiErrorCode;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.ScrapSubstrateLogQAMapper;
import com.mt.roll.wms.params.ScrapSubstrateLogParam;
import com.mt.roll.wms.params.ScrapSubstrateLogQueryParam;
import com.mt.roll.wms.service.ScrapSubstrateLogService;
import com.mt.roll.wms.vo.ScrapSubstrateLogVo;
import com.mt.roll.wms.vo.ScrapSubstrateReportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2021/3/10 10:20
 */
@Service
@Transactional
public class ScrapSubstrateLogServiceImpl extends BaseSupport implements ScrapSubstrateLogService {

    @Autowired
    ScrapSubstrateLogServiceBiz serviceBiz;

    @Autowired
    ScrapSubstrateLogQAMapper mapper;

    @Override
    public R<IPage<ScrapSubstrateLogVo>> findAll(ScrapSubstrateLogQueryParam scrapSubstrateLogQueryParam) {
        IPage<ScrapSubstrateLogVo> iPage = mapper.findAll(
                new Page<>(scrapSubstrateLogQueryParam.getCurrent(), scrapSubstrateLogQueryParam.getSize()),
                scrapSubstrateLogQueryParam);
        return successful(iPage);
    }

    @Override
    public R<Boolean> create(ScrapSubstrateLogParam scrapSubstrateLogParam) {
        ScrapSubstrateLog scrapSubstrateLog = new ScrapSubstrateLog();
        BeanUtils.copyProperties(scrapSubstrateLogParam, scrapSubstrateLog);
        boolean save = serviceBiz.save(setCommonField(scrapSubstrateLog));

        if(save){
            return successful(save);
        }else {
            return failed(IErrorCode.CODE_FAILED,"创建失败");
        }
    }

    @Override
    public R<Boolean> update(ScrapSubstrateLogParam scrapSubstrateLogParam) {
        ScrapSubstrateLog scrapSubstrateLog = new ScrapSubstrateLog();
        BeanUtils.copyProperties(scrapSubstrateLogParam, scrapSubstrateLog);

        boolean res = serviceBiz.updateById(setCommonField(scrapSubstrateLog));
        if(res){
            return successful(res);
        }else {
            return failed(IErrorCode.CODE_FAILED,"更新失败");
        }
    }

    @Override
    public R<Boolean> delete(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        boolean res = serviceBiz.removeById(idParam.getId());
        if(res){
            return successful(res);
        }else {
            return failed(IErrorCode.CODE_FAILED,"删除失败");
        }
    }

    @Override
    public R<ScrapSubstrateLog> findAllById(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        ScrapSubstrateLog byId = serviceBiz.getById(idParam.getId());
        if(byId!=null){
            return successful(byId);
        }
        return failed(IErrorCode.CODE_FAILED,"查找的内容不存在");
    }

    @Override
    public R<IPage<ScrapSubstrateReportVo>> findReportByEquipmentId(ScrapSubstrateLogQueryParam scrapSubstrateLogQueryParam) {
        IPage<ScrapSubstrateReportVo> iPage = mapper.findReportByEquipmentId(
                new Page<>(scrapSubstrateLogQueryParam.getCurrent(), scrapSubstrateLogQueryParam.getSize()),
                scrapSubstrateLogQueryParam);
        return successful(iPage);
    }
}
